package com.key.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.common.base.RespResult;
import com.key.common.base.TranException;
import com.key.common.constants.Constants;
import com.key.modules.entity.ICustomers;
import com.key.modules.mapper.ICustomersMapper;
import com.key.modules.dto.ICConditionDTO;
import com.key.modules.dto.ICustomersDTO;
import com.key.modules.service.ICustomersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 *
 *
 * @author lrm
 * @since 2023-03-23 18:09:28
 */
@Service
@Slf4j
public class ICustomersServiceImpl implements ICustomersService {

    @Autowired
    private ICustomersMapper iCustomersMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public RespResult addICustomers(ICustomersDTO iCustomersDTO) {

       // log.info("{}==添加个人客户", SecurityUtils.getSubject().getPrincipal());
        Long i_no = redisTemplate.opsForValue().increment("i_no");
        ICustomers iCustomers=new ICustomers();
        BeanUtil.copyProperties(iCustomersDTO,iCustomers);
        iCustomers.setINo(String.format("%s%05d", Constants.ICPRE, i_no));
        iCustomers.setCreateTime(new Date());
        int i = iCustomersMapper.insert(iCustomers);
        if (i>0){
            log.info("{}==个人客户添加成功", iCustomers.toString());
            return RespResult.success();
        }
        return RespResult.error("添加失败");

    }

    @Override
    public RespResult findAllICustomers(ICConditionDTO icConditionDTO) {
        Page<ICustomers> page=new Page<>(icConditionDTO.getPageNum(),icConditionDTO.getPageSize());
        LambdaQueryWrapper<ICustomers> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StrUtil.isNotBlank(icConditionDTO.getIName()),ICustomers::getIName,icConditionDTO.getIName())
                .eq(StrUtil.isNotBlank(icConditionDTO.getPhone()),ICustomers::getPhone,icConditionDTO.getPhone())
                .ge(icConditionDTO.getBeginTime()!=null,ICustomers::getCreateTime,icConditionDTO.getBeginTime())
                .le(icConditionDTO.getEndTime()!=null,ICustomers::getCreateTime,icConditionDTO.getEndTime())
                .eq(icConditionDTO.getIAState()!=null,ICustomers::getIAState,icConditionDTO.getIAState())
                .orderBy(icConditionDTO.getOrder()!=null,icConditionDTO.getOrder()==Constants.ASC,ICustomers::getCreateTime);
        Page<ICustomers> iCustomersPage = iCustomersMapper.selectPage(page,lambdaQueryWrapper);
        System.out.println("当前页码数："+iCustomersPage.getCurrent());
        System.out.println("每页显示条数："+iCustomersPage.getSize());
        System.out.println("总条数："+iCustomersPage.getTotal());
        System.out.println("总页数："+iCustomersPage.getPages());
        System.out.println("查询数据："+iCustomersPage.getRecords());
        return RespResult.success(iCustomersPage);
    }

    @Override
    public RespResult updateICustomers(ICustomersDTO iCustomersDTO) {
        // log.info("{}==修改个人客户", SecurityUtils.getSubject().getPrincipal());
        ICustomers iCustomers=new ICustomers();
        BeanUtil.copyProperties(iCustomersDTO,iCustomers);
        int i = iCustomersMapper.updateById(iCustomers);
        if (i>0){
            log.info("{}==修改成功", iCustomers.toString());
            return RespResult.success();
        }
        return RespResult.error("修改失败");
    }

    @Override
    @Transactional
    public RespResult deleteICustomers(List<Integer> ids) {
        // log.info("{}==删除个人客户", SecurityUtils.getSubject().getPrincipal());
        try {
            iCustomersMapper.deleteBatchIds(ids);
            log.info("{}==删除成功",ids);
            return RespResult.success();
        }catch (Exception e){
            throw new TranException("删除失败");
        }

    }

    @Override
    public RespResult findICustomersById(Integer id) {
        ICustomers iCustomers = iCustomersMapper.selectById(id);
        if (iCustomers!=null){
            return RespResult.success(iCustomers);
        }
        return RespResult.error("查询失败");
    }

    @Override
    @Transactional
    public RespResult updateIAState(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                ICustomers iCustomers=new ICustomers();
                iCustomers.setId(id);
                iCustomers.setIAState(Constants.IASTATE);
                iCustomers.setAuditTime(new Date());
                iCustomersMapper.updateById(iCustomers);
            }
            return RespResult.success();
        }catch (Exception e){
            throw new TranException("审核失败");
        }
    }

    @Override
    @Transactional
    public RespResult disDSta(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                ICustomers iCustomers=new ICustomers();
                iCustomers.setId(id);
                iCustomers.setIState(Constants.DISABLEDSTA);
                iCustomersMapper.updateById(iCustomers);
            }
            return RespResult.success();
        }catch (Exception e){
            throw new TranException("禁用失败");
        }
    }

    @Override
    @Transactional
    public RespResult enDSta(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                ICustomers iCustomers=new ICustomers();
                iCustomers.setId(id);
                iCustomers.setIState(Constants.ENABLEDSTA);
                iCustomersMapper.updateById(iCustomers);
            }
            return RespResult.success();
        }catch (Exception e){
            throw new TranException("启用失败");
        }
    }
}

